import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_framework/assets/r_img.dart';
import 'package:flutter_framework/extens/str_ext.dart';
import 'package:flutter_framework/routes/home/home_routes.dart';
import 'package:flutter_framework/utils/logs.dart';
import 'package:flutter_framework/utils/screen.dart';
import 'package:flutter_framework/utils/screen_ui.dart';
import 'package:flutter_framework/views/Interactive_viewer_page/Interactive_viewer_page.dart';
import 'package:flutter_framework/views/animated_breath/animated_breath.dart';
import 'package:flutter_framework/views/animated_custom/animated_custom.dart';
import 'package:flutter_framework/views/animated_icon_btn/animated_icon_btn.dart';
import 'package:flutter_framework/views/animated_num/animated_num.dart';
import 'package:flutter_framework/views/animated_swicher/animated_swicher.dart';
import 'package:flutter_framework/views/animated_transaction/animated_transaction.dart';
import 'package:flutter_framework/views/animted_snow/animated_snow.dart';
import 'package:flutter_framework/views/canvas_demo/canvas_demo.dart';
import 'package:flutter_framework/views/cao_card/cao_3d_studio.dart';
import 'package:flutter_framework/views/curve_interval/curve_interval.dart';
import 'package:flutter_framework/views/custom_draw_path/custom_draw_path.dart';
import 'package:flutter_framework/views/custom_provider/custom_provider.dart';
import 'package:flutter_framework/views/debug/ctrl/network_debug_ctrl.dart';
import 'package:flutter_framework/views/drag_view_photo/drag_view_photo.dart';
import 'package:flutter_framework/views/elastic_drawer/elastic_drawer.dart';
import 'package:flutter_framework/views/home/ctrl/home_ctrl.dart';
import 'package:flutter_framework/views/home/models/dev_item_model.dart';
import 'package:flutter_framework/views/home/widgets/find_item.dart';
import 'package:flutter_framework/views/layout_builder/layout_builder.dart';
import 'package:flutter_framework/views/list_wheel_scroll_view/list_wheel_scroll_view.dart';
import 'package:flutter_framework/views/open_container/open_container.dart';
import 'package:flutter_framework/views/page_view/page_view.dart';
import 'package:flutter_framework/views/red_book_user/red_book_user.dart';
import 'package:flutter_framework/views/reorder_scrollview/reorder_scrollview.dart';
import 'package:flutter_framework/views/sin_box/sin_box.dart';
import 'package:flutter_framework/views/sort_color_box/sort_color_box.dart';
import 'package:flutter_framework/views/stream_builder_page/stream_builder_page.dart';
import 'package:flutter_framework/views/swicher_img/swicher_img.dart';
import 'package:flutter_framework/views/test_changenotifier/test_changenotifier.dart';
import 'package:flutter_framework/views/test_close_kb_bg_dialog/test_close_kb_bg_dialog.dart';
import 'package:flutter_framework/views/test_custom_paint/custom_paper.dart';
import 'package:flutter_framework/views/test_custom_paint/paper_status.dart';
import 'package:flutter_framework/views/test_inheritwidget/test_inheritwidget.dart';
import 'package:flutter_framework/views/test_isolate/test_isolate.dart';
import 'package:flutter_framework/views/test_json_ser/test_json_ser.dart';
import 'package:flutter_framework/views/test_markneedbuild/test_markneedbuild.dart';
import 'package:flutter_framework/views/test_multi_child_layout_deleget/test_multi_child_layout_delegate.dart';
import 'package:flutter_framework/views/test_parent_data/test_parent_data.dart';
import 'package:flutter_framework/views/test_svg_path/test_svg_path.dart';
import 'package:flutter_framework/views/transform_box/transform_box.dart';
import 'package:flutter_framework/views/transform_card/transform_card.dart';
import 'package:flutter_framework/views/zflutter_card/zflutter_card.dart';
import 'package:get/get.dart';
import '../debug/debug_view.dart';
import '../test_multi_scroll/test_multi_scroll.dart';
import '../timeout_btn/timeout_btn.dart';

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final HomeCtrl _ctrl = Get.put(HomeCtrl());

  Widget _buildItem(DevItemModel dItem, {void Function()? onTap}) {
    final DevItemModel item = dItem;
    return Container(
      decoration: BoxDecoration(
          border: Border(bottom: BorderSide(color: Colors.grey.shade300))),
      child: ListTile(
        leading: Icon(item.icon),
        title: Text(item.title),
        onTap: onTap ??
            () {
              if (item.url.isNotEmpty) {
                Get.toNamed(item.url);
              }
            },
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    /// 初始化屏幕宽度
    Screen.initScreen(context);
    ScreenUI.initScreenUI(context);
    // return StreamBuilderPage();
    return Scaffold(
      appBar: AppBar(
        title: const Text('调试项目'),
        centerTitle: true,
      ),
      body: Center(
        child: ListView(
          children: [
            Hero(
              tag: "wx_icon_hero",
              child: Image.asset(
                ImgR.imgUserserWxIcon,
                width: 50,
                height: 50,
              ),
            ),
            _buildItem(
                DevItemModel(
                    title: '测试JsonSer',
                    url: '',
                    icon: Icons.animation), onTap: () {
              Get.to(TestJsonSer());
            }),
            _buildItem(
                DevItemModel(title: '小红书首页', url: '', icon: Icons.animation),
                onTap: () {
              Get.to(RedBookUser());
            }),
            _buildItem(
                DevItemModel(
                    title: 'test parent Data',
                    url: '',
                    icon: Icons.animation), onTap: () {
              Get.to(TestParentData());
            }),
            _buildItem(
                DevItemModel(
                    title: 'test  mark needs build',
                    url: '',
                    icon: Icons.animation), onTap: () {
              Get.to(TestMarkNeedsBuild());
            }),
            _buildItem(
                DevItemModel(
                    title: 'test inherit widget',
                    url: '',
                    icon: Icons.animation), onTap: () {
              Get.to(TestInheritWidget());
            }),
            _buildItem(
                DevItemModel(
                    title: 'changenotifier demo',
                    url: '',
                    icon: Icons.animation), onTap: () {
              Get.to(TestChangeNotifier());
            }),
            _buildItem(
                DevItemModel(
                    title: '点击背景关闭弹窗键盘冲突解决',
                    url: '',
                    icon: Icons.animation), onTap: () {
              Get.to(TestCloseKBBGDialog());
            }),
            _buildItem(
                DevItemModel(
                    title: '图片拖拽关闭demo',
                    url: '',
                    icon: Icons.animation), onTap: () {
              // showDialog(
              //     context: context,
              //     barrierDismissible: true,
              //     useSafeArea: false,
              //     barrierColor: Colors.transparent,
              //     builder: (ctx) {
              //       return
              //     });
              Navigator.of(context).push(
                PageRouteBuilder(
                  // 动画持续时间为500毫秒
                  pageBuilder: (context, animation1, animation2) {
                    return DragViewPhoto();
                  },
                  fullscreenDialog: true,
                  // 如果需要全屏对话框效果，可以设置为true
                  settings: RouteSettings(
                    arguments: {}, // 可选的路由参数
                  ),
                  maintainState: false,
                  // 是否保持状态
                  opaque: false,
                  // 背景是否透明
                  barrierColor: Colors.transparent,
                  // 遮罩颜色和透明度，这里设置为半透明黑色
                  barrierDismissible: false, // 是否可以通过点击遮罩关闭路由
                ),
              );
            }),
            _buildItem(
                DevItemModel(title: '多页面滚动优化', url: '', icon: Icons.animation),
                onTap: () {
              Get.to(TestMultilScroll(title: "多页面滚动优化"));
            }),
            _buildItem(
                DevItemModel(
                    title: 'multiChildLayoutDelegat',
                    url: '',
                    icon: Icons.animation), onTap: () {
              Get.to(TestMultiChildLayout());
            }),
            _buildItem(
                DevItemModel(
                    title: 'test svg path',
                    url: '',
                    icon: Icons.animation), onTap: () {
              Get.to(TestSvgPath());
            }),
            _buildItem(
              DevItemModel(title: '画布的变换和状态 ', url: '', icon: Icons.animation),
              onTap: () {
                Get.to(PaperStatus())?.then((value) {
                  SystemChrome.setPreferredOrientations(// 使设备横屏显示
                      [
                    DeviceOrientation.portraitUp,
                    DeviceOrientation.portraitDown
                  ]);
                });
              },
            ),
            _buildItem(
                DevItemModel(title: '画笔 基础属性 ', url: '', icon: Icons.animation),
                onTap: () {
              Get.to(CustomPaper());
            }),
            _buildItem(
                DevItemModel(
                    title: '测试 isolate',
                    url: '',
                    icon: Icons.animation), onTap: () {
              Get.to(TestIsolate());
            }),
            _buildItem(
                DevItemModel(title: '悬浮盒子', url: '', icon: Icons.animation),
                onTap: () {
              Get.to(SinBox());
            }),
            _buildItem(
                DevItemModel(title: '渐变背景色', url: '', icon: Icons.animation),
                onTap: () {
              Get.to(CustomDrawPath());
            }),
            // _buildItem(
            //     DevItemModel(
            //         title: 'z flutter card 11',
            //         url: '',
            //         icon: Icons.animation), onTap: () {
            //   Get.to(CardReal3DDemoPage());
            // }),
            _buildItem(
                DevItemModel(
                    title: 'oepn container',
                    url: '',
                    icon: Icons.animation), onTap: () {
              Get.to(OpenContainerPage());
            }),
            _buildItem(
                DevItemModel(
                    title: 'animated icon btn',
                    url: '',
                    icon: Icons.animation), onTap: () {
              Get.to(AnimatedIconBtn());
            }),
            _buildItem(
                DevItemModel(
                    title: '自定义provider',
                    url: '',
                    icon: Icons.animation), onTap: () {
              Get.to(CustomProvider());
            }),
            _buildItem(
                DevItemModel(title: '曹操卡片', url: '', icon: Icons.animation),
                onTap: () {
              Get.to(Cao3DStudio());
            }),
            _buildItem(
                DevItemModel(title: '弹性抽屉', url: '', icon: Icons.animation),
                onTap: () {
              Get.to(ElasticDrawerDemo());
            }),
            _buildItem(
                DevItemModel(
                    title: 'animather img',
                    url: '',
                    icon: Icons.animation), onTap: () {
              Get.to(AnimatedSwitcherImg());
            }),
            _buildItem(
                DevItemModel(
                    title: 'sort color box',
                    url: '',
                    icon: Icons.animation), onTap: () {
              Get.to(const SortColorBox());
            }),
            _buildItem(
                DevItemModel(
                    title: 'canvas demo',
                    url: '',
                    icon: Icons.animation), onTap: () {
              Get.to(const CanvasDemo());
            }),
            _buildItem(
                DevItemModel(
                    title: 'time out btn',
                    url: '',
                    icon: Icons.animation), onTap: () {
              Get.to(const TimeoutBtn());
            }),
            _buildItem(
                DevItemModel(
                    title: 'transform box',
                    url: '',
                    icon: Icons.animation), onTap: () {
              Get.to(const TransformBox());
            }),
            _buildItem(
                DevItemModel(
                    title: 'transform card',
                    url: '',
                    icon: Icons.animation), onTap: () {
              Get.to(const TransformCard());
            }),
            _buildItem(
                DevItemModel(title: '雪人下雪动画', url: '', icon: Icons.animation),
                onTap: () {
              Get.to(const AnimatedSnow());
            }),
            _buildItem(
                DevItemModel(title: '呼吸动效', url: '', icon: Icons.animation),
                onTap: () {
              Get.to(const AnimatedBreath());
            }),
            _buildItem(
                DevItemModel(title: '自定义动画', url: '', icon: Icons.animation),
                onTap: () {
              Get.to(const AnimatedCustom());
            }),
            _buildItem(
                DevItemModel(
                    title: '使用 curve interval 做交错动画 ',
                    url: '',
                    icon: Icons.animation), onTap: () {
              Get.to(const CurveIntervalPage());
            }),
            _buildItem(
                DevItemModel(
                    title: 'transaction ',
                    url: '',
                    icon: Icons.animation), onTap: () {
              Get.to(const AnimatedTransactionPage());
            }),
            _buildItem(
                DevItemModel(
                    title: 'steam builder',
                    url: '',
                    icon: Icons.switch_camera), onTap: () {
              Get.to(StreamBuilderPage());
            }),
            _buildItem(
                DevItemModel(
                    title: '动画计数器组件',
                    url: '',
                    icon: Icons.switch_camera), onTap: () {
              Get.to(AnimatedNum());
            }),
            _buildItem(
                DevItemModel(
                    title: 'animated swicher',
                    url: '',
                    icon: Icons.switch_camera), onTap: () {
              Get.to(AnimatedSwicherPage());
            }),
            _buildItem(
                DevItemModel(
                    title: 're order scroll view',
                    url: '',
                    icon: Icons.image), onTap: () {
              Get.to(ReOrderScrollViewPage());
            }),
            _buildItem(
                DevItemModel(
                    title: 'page view',
                    url: HomeRouter.testImg,
                    icon: Icons.image), onTap: () {
              Get.to(PageViewPage());
            }),
            _buildItem(
                DevItemModel(
                    title: 'list wheel scroll view',
                    url: HomeRouter.testImg,
                    icon: Icons.image), onTap: () {
              Get.to(ListWheelScrollViewPage());
            }),
            _buildItem(
                DevItemModel(
                    title: 'interactive viewer',
                    url: HomeRouter.testImg,
                    icon: Icons.image), onTap: () {
              Get.to(InteractiveViewerPage());
            }),
            _buildItem(
                DevItemModel(
                    title: 'layout builder',
                    url: HomeRouter.testImg,
                    icon: Icons.image), onTap: () {
              Get.to(LayOutBuilderPage());
            }),
            _buildItem(
              DevItemModel(
                  title: '图片测试', url: HomeRouter.testImg, icon: Icons.image),
            ),
            _buildItem(
              DevItemModel(
                  title: '添加购物车动画',
                  url: HomeRouter.addCartAnimate,
                  icon: Icons.shopping_cart),
            ),
            _buildItem(
              DevItemModel(
                  title: '多语言', url: HomeRouter.language, icon: Icons.language),
            ),
            _buildItem(
              DevItemModel(
                  title: '主题色',
                  url: HomeRouter.testTheme,
                  icon: Icons.language),
            ),
            _buildItem(
                DevItemModel(title: '网络弹窗显示', url: '', icon: Icons.language),
                onTap: () {
              if (!NetworkDebugCtrl.to.isShow.value) {
                NetworkDebugCtrl.to.show(context);
              }
            }),
            _buildItem(
              DevItemModel(
                  title: '设备信息检测',
                  url: HomeRouter.deviceInfo,
                  icon: Icons.language),
            ),
            _buildItem(DevItemModel(
                title: 'widget组件',
                url: HomeRouter.widgetDemo,
                icon: Icons.widgets)),
            _buildItem(DevItemModel(
                title: '自定义 controller',
                url: HomeRouter.genController,
                icon: Icons.control_camera)),
            _buildItem(DevItemModel(
                title: 'test inherited',
                url: HomeRouter.testInerited,
                icon: Icons.travel_explore_sharp)),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _ctrl.increment,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

class FadeRoute<T> extends MaterialPageRoute<T> {
  FadeRoute({
    required super.builder,
    super.settings,
  });

  @override
  Widget buildTransitions(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation, Widget child) {
    // 创建渐变效果
    return FadeTransition(
      opacity: animation,
      child: child,
    );
  }
}
